const DB = require('../utils/db')

module.exports = {
  /**
   * 获取指定movie id的所有评论
   */
  list: async ctx => {
    const movieId = +ctx.request.query.movieId;
    let comments = []
    if (!isNaN(movieId)) {
      comments = await DB.query(
        'select id, movie_comment.movie_id as `movieId`, comment_type as `commentType`, avatar, content, rating, movie_comment.user, username, movie_comment.create_time as `createTime`, count(comment_like.user) as `likeCount` from movie_comment left join comment_like on movie_comment.id = comment_like.comment_id left join comment_fave on movie_comment.id = comment_fave.comment_id where movie_comment.movie_id = ? group by movie_comment.id order by movie_comment.create_time desc', [movieId]);
    }
    ctx.state.data = comments;

  },

  /**
   * 获取指定comment id的评论详情
   */
  detail: async ctx => {
    const commentId = +ctx.params.id;
    let comment = {};
    if (!isNaN(commentId)) {
      comment = (await DB.query('select movie_comment.id as `id`, movie.id as `movieId`, movie.title as `movieTitle`, movie.image as `movieImage`, comment_type as `commentType`, avatar, content, rating, movie_comment.user, username, movie_comment.create_time as `createTime`, count(comment_like.user) as `likeCount` from movie_comment join movie on movie.id = movie_comment.movie_id left join comment_like on movie_comment.id = comment_like.comment_id left join comment_fave on movie_comment.id = comment_fave.comment_id where movie_comment.id = ? group by movie_comment.id', [commentId]))[0] || null;
    }
    ctx.state.data = comment;
  },

  /**
   * 添加评论
   */
  add: async ctx => {
    let user = ctx.state.$wxInfo.userinfo.openId;
    let username = ctx.state.$wxInfo.userinfo.nickName;
    let avatar = ctx.state.$wxInfo.userinfo.avatarUrl;
    let content = ctx.request.body.content;
    let movieId = +ctx.request.body.movieId;
    let rating = +ctx.request.body.rating;
    let commentType = +ctx.request.body.commentType;


    if (!isNaN(movieId)) {
      await DB.query('INSERT INTO movie_comment(movie_id, comment_type, rating, user, username, avatar, content) VALUES (?, ?, ?, ?, ?, ?, ?)', [movieId, commentType, rating, user, username, avatar, content]);
    }

    ctx.state.data = {}
  },

  /**
   * 获取指定movie id的我的影评
   */
  myComment: async ctx => {
    let user = ctx.state.$wxInfo.userinfo.openId;
    let movieId = +ctx.request.query.movieId;
    if (!isNaN(movieId)) {
      ctx.state.data = (await DB.query('select movie_comment.id , movie_comment.user, movie_comment.create_time as `createTime` from movie_comment where movie_comment.movie_id = ? and movie_comment.user = ?', [movieId, user]))[0] || null;
    } else {
      ctx.state.code = -1;
    }
  },

  /**
   * 获取推荐影评(不会推荐自己的影评和已收藏的影评给自己)
   * 随机推荐1部较新的影评
   * 随机推荐1条点赞数多的影评
   * 随机推荐1部评分高电影的影评
   */
  recommend: async ctx => {
    const me = ctx.state.$wxInfo.userinfo.openId;
    const recommends = []

    const m_ids = await DB.query('SELECT c.movie_id, COUNT(c.rating) AS rate_count FROM movie_comment AS c WHERE c.rating > 3 GROUP BY c.movie_id, c.rating ORDER BY RAND() DESC LIMIT 5');

    const ids = m_ids.reduce(function (item, cur) {
      return (item !== '' ? `${item}, ` : `${item}`) + cur.movie_id.toString()
    }, '')

    const recommend_movie_info = await DB.query(`SELECT 
        DISTINCT (m.id) AS m_id,
          m.title AS m_title,
          m.image AS m_image,
          c.id AS c_id,
          c.comment_type,
          c.rating,
          c.user,
          c.username,
          c.avatar,
          c.content,
          c.create_time
      FROM 
      movie AS m
      INNER JOIN
      movie_comment as c
      ON
      m.id = c.movie_id
      WHERE m.id in (${ids}) AND c.rating = 5
      LIMIT 3`);



    ctx.state.data = recommend_movie_info
  }
}